package com.neusoft.controller;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Credit;
import com.neusoft.service.CreditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RefreshScope
@RestController
@RequestMapping("/CreditController")
public class CreditController {
	@Autowired
	private CreditService creditService;

	@GetMapping("/getTotalCredit/{userId}")
	public CommonResult<Integer> getTotalCreditByUserId(@PathVariable String userId) throws Exception {
		return new CommonResult<Integer>(200, "success", creditService.getTotalCreditByUserId(userId));
	}

	@GetMapping("/getAvailableCredit/{userId}")
	public CommonResult<List<Credit>> getAvailableCreditByUserId(@PathVariable String userId) throws Exception {
		return new CommonResult<List<Credit>>(200, "success", creditService.getAvailableCreditByUserId(userId));
	}

	@GetMapping("/getCreditByUserId/{userId}")
	public CommonResult<List<Credit>> getCreditByUserId(@PathVariable String userId) throws Exception {
		return new CommonResult<List<Credit>>(200, "success", creditService.getCreditByUserId(userId));
	}

	@GetMapping("/getCreditByUserIdAndChannelType/{userId}/{channelType}")
	public CommonResult<List<Credit>> getCreditByUserIdAndChannelType(@PathVariable String userId,
			@PathVariable Integer channelType) throws Exception {
		return new CommonResult<List<Credit>>(200, "success",
				creditService.getCreditByUserIdAndChannelType(userId, channelType));
	}

	@PostMapping("/insertCredit/{userId}/{num}/{channelType}")
	public CommonResult<Integer> insertCredit(Credit credit) throws Exception {
		return new CommonResult<Integer>(200, "success", creditService.insertCredit(credit));
	}

	@PostMapping("/consumeCredit/{userId}/{num}")
	public CommonResult<Integer> consumeCredit(Credit credit) throws Exception {
		return new CommonResult<Integer>(200, "success", creditService.consumeCredit(credit));
	}
}
